unit DataSetUtils;

interface

uses SysUtils, Classes, Db, DBGrids;

procedure CopyFields(Src, Dest: TDataSet);
procedure CopyData(Src, Dest: TDataSet; FilterField: string = '');
function SetFieldVal(DS: TDataSet; FieldName: string; NewVal: variant): boolean;
//
function LookForValue(DS: TDataSet; KeyField: string; KeyValue: variant): boolean;
//
procedure ShowColumns(Grid: TDBGrid; ColNames: string);

implementation

function SetFieldVal(DS: TDataSet; FieldName: string; NewVal: variant): Boolean;
var fld: TField;
begin
  result := False;
  fld := DS.FindField(FieldName);
  if fld <> nil then begin
    fld.Value := NewVal;
    result := True;
  end;
end;

procedure ShowColumns(Grid: TDBGrid; ColNames: string);
var
  sl: TStringList;
  i, p: integer;
begin
  sl := TStringList.Create;
  try
    sl.CommaText := ColNames;
    for i := 0 to sl.Count - 1 do
      with TColumn.Create(Grid.Columns) do begin
        p := Pos(':', sl[i]);
        if p > 0 then begin
          FieldName := Copy(sl[i], 1, p-1);
          Width := StrToInt(Copy(sl[i], p+1, Length(sl[i])));
        end else
          FieldName := sl[i];
      end;
  finally
    sl.Free;
  end;  
end;

function LookForValue(DS: TDataSet; KeyField: string; KeyValue: variant): boolean;
begin
  result := False;
  Ds.Next;
  while not DS.Eof do begin
    if DS[KeyField] = KeyValue then begin
      result := True;
      Break;
    end
    else
      DS.Next;
  end;
end;

procedure CopyFields(Src, Dest: TDataSet);
var
  i: integer;
  src_fld, fld: TField;
  FldClass: TFieldClass;
begin
  for i := 0 to Src.Fields.Count - 1 do begin
    src_fld := Src.Fields[i];
    FldClass := DefaultFieldClasses[src_fld.DataType];
    fld := FldClass.Create(Dest.Owner);
    fld.FieldKind := src_fld.FieldKind;
    if fld.FieldKind = fkLookUp then begin
      Fld.LookupDataset := src_fld.LookupDataset;
      Fld.KeyFields := src_fld.KeyFields;
      Fld.LookupKeyFields := src_fld.LookupKeyFields;
      Fld.LookupResultField := src_fld.LookupResultField;
    end;
    fld.FieldName := src_fld.FieldName;
    fld.Size := src_fld.Size;
    fld.Index := src_fld.Index;
    fld.DisplayLabel := src_fld.DisplayLabel;
    fld.DisplayWidth := src_fld.DisplayWidth;
    if fld is TBCDField then
      TBCDField(fld).DisplayFormat := TBCDField(src_fld).DisplayFormat;
    fld.Visible := src_fld.Visible;
    fld.DataSet := Dest;
  end;
  Dest.OnCalcFields := Src.OnCalcFields;
end;

procedure CopyRec(Src, Dest: TDataSet);
var
  i: integer;
  DestFld: TField;
begin
  Dest.Append;
  for i := 0 to Src.Fields.Count - 1 do
    if Src.Fields[i].FieldKind = fkData then begin
      DestFld := Dest.FindField(Src.Fields[i].FieldName);
      if DestFld <> nil then
        DestFld.Value := Src.Fields[i].Value;
    end;
  Dest.Post;
end;

procedure CopyData(Src, Dest: TDataSet; FilterField: string);
begin
  Src.First;
  while not Src.Eof do begin
    if Src.FieldByName(FilterField).AsBoolean then
      CopyRec(Src, Dest);
    Src.Next;
  end;
end;


end.
